package cn.wolfcode.Controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;


    @RequirePermission(name = "结算单页面",experssion = "consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }

    @RequestMapping("/delete")
@RequirePermission(name = "结算单页面",experssion = "consumption:list")
    @ResponseBody
    public jsonResult delete(Long id){
        if (id != null) {
            consumptionService.delete(id);
        }
        return new jsonResult();
    }


    @RequestMapping("/saveOrUpdate")
@RequirePermission(name = "结算单页面",experssion = "consumption:list")
    @ResponseBody
    public jsonResult saveOrUpdate(Consumption consumption){
        if (consumption.getId() != null) {

            consumptionService.update(consumption);
        }else {
            consumptionService.save(consumption);
        }
        return new jsonResult();
    }


    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private IBusinessService businessService;

    @RequirePermission(name = "结算明细单页面",experssion = "consumptionItem:list")
    @RequestMapping("/input")
    public String input(Model model,Long id){
        //因为结算单和结算明细通过（流水号）进行关联，所以我们先通过流水号找到明细表对应的数据
        //返回结算单明细的信息给input页面进行回显
        //根据页面的数据，我们可以知道有业务大类，业务小类，结算类型   售卖，二手至，服务费对应的全是业务小类表
        //因此我们需要连表后找到他们的title或者name进行封装
        if (id !=null){
            Consumption consumption = consumptionService.get(id);
            List<ConsumptionItem> items = consumptionItemService.selectForCno(consumption.getCno());
            model.addAttribute("consumption", consumption);
            model.addAttribute("items", items);
        }
        model.addAttribute("businesses", businessService.listAll());
        return "consumption/input";
    }


    @Autowired
    private IAppointmentService appointmentService;
    @RequestMapping("/save")
    @RequirePermission(name = "创建结算单操作",experssion = "consumption:save")
    @ResponseBody
    public jsonResult save(String appointmentAno){
        jsonResult Result = new jsonResult();
            Consumption consumption = consumptionService.save(appointmentAno);
            Result.setData(consumption);
        return Result;
    }
}
